//
//  HomeController.m
//  MyBank
//
//  Created by 崔宝印 on 2017/7/26.
//  Copyright © 2017年 Koolearn. All rights reserved.
//

#import "HomeController.h"
#import "AccountCell.h"
#import "CoreDataHelper.h"
#import "UIColor+BYColor.h"

@interface HomeController ()

@property (weak, nonatomic) IBOutlet UILabel *assetLabel;
@property (weak, nonatomic) IBOutlet UILabel *NALabel;
@property (weak, nonatomic) IBOutlet UILabel *debtLabel;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property (strong, nonatomic) NSMutableArray *sectionArray;

@property (strong, nonatomic) NSArray *creditCardArray;

@end

@implementation HomeController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.sectionArray = [NSMutableArray array];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
    [self.sectionArray removeAllObjects];
    
    self.creditCardArray = [[CoreDataHelper sharedInstance] fetchEntityWith:@"CreditCard"];
    if (self.creditCardArray != nil && self.creditCardArray.count > 0)
    {
        NSDictionary *dic = @{@"type":@"信用卡",@"data":self.creditCardArray};
        [self.sectionArray addObject:dic];
    }
    
    [self.myTableView reloadData];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = self.sectionArray[section];
    NSArray *dataArray = dic[@"data"];
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indentifier = @"AccountCell";
    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier forIndexPath:indexPath];
    
    NSDictionary *dic = self.sectionArray[indexPath.section];
    NSArray *dataArray = dic[@"data"];
    [cell resetSubviewWithDataModel:dataArray[indexPath.row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"BankSelectFinished" object:self.bankArray[indexPath.row]];
//    [self goBackAction:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary *dic = self.sectionArray[section];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30.0f)];
    view.backgroundColor = [UIColor colorWithHexString:@"#f7f7f7"];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, view.frame.size.width - 15, 30.0f)];
    label.backgroundColor = [UIColor clearColor];
    label.text = dic[@"type"];
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor colorWithHexString:@"#4D5261"];
    [view addSubview:label];
    return view;
}
@end
